/* Copyright Statement:
*
* This software/firmware and related documentation ("MediaTek Software") are
* protected under relevant copyright laws. The information contained herein
* is confidential and proprietary to MediaTek Inc. and/or its licensors.
* Without the prior written permission of MediaTek inc. and/or its licensors,
* any reproduction, modification, use or disclosure of MediaTek Software,
* and information contained herein, in whole or in part, shall be strictly prohibited.
*/
/* MediaTek Inc. (C) 2015. All rights reserved.
*
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
* THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
* CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
* SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
* CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*/

#include <debug.h>
#include <malloc.h>
#include <string.h>
#include <platform/mt_typedefs.h>
#include <platform/rsa_oaep.h>
#include <platform/rsa2048_export.h>

#define RSA_TST_KEY_N     0xae, 0xd5, 0x2d, 0x35, 0xe7, 0x2e, 0x9d, 0xb1, 0x27, 0x78, 0x6c, 0xcc, 0xe5, 0x62, 0xba, 0xe2, \
                          0xa0, 0xa0, 0x4a, 0xbb, 0x7a, 0xbe, 0x27, 0x93, 0xa5, 0xe7, 0x92, 0x00, 0x53, 0x46, 0x28, 0x3f, \
                          0xc0, 0x4f, 0x87, 0x11, 0x05, 0x27, 0x86, 0x3d, 0xbb, 0x73, 0xe8, 0x65, 0xa9, 0x71, 0x24, 0xad, \
                          0xf8, 0x01, 0x28, 0xcc, 0x58, 0x37, 0x57, 0x49, 0xb4, 0x70, 0x99, 0x33, 0xcc, 0x21, 0x25, 0x11, \
                          0x1f, 0x07, 0xab, 0xb0, 0xe5, 0x0b, 0x0a, 0x3f, 0x5f, 0x91, 0x59, 0xc3, 0x1d, 0xfc, 0x9b, 0xb3, \
                          0xbd, 0xa3, 0x6b, 0x8a, 0xb4, 0x9f, 0x93, 0x48, 0x00, 0x9e, 0xdc, 0x10, 0xc5, 0x78, 0xf4, 0x99, \
                          0xc0, 0xff, 0xa1, 0xc8, 0xf3, 0xb7, 0x3a, 0x52, 0xfa, 0x20, 0x5f, 0x3a, 0x5c, 0x38, 0x0b, 0x8d, \
                          0x12, 0x49, 0xb6, 0x41, 0x4a, 0xbf, 0xaa, 0x72, 0xd8, 0x1a, 0x97, 0xb6, 0x8e, 0x58, 0xd5, 0xc3, \
                          0x0f, 0x06, 0x8f, 0xd6, 0x85, 0x48, 0xba, 0x3f, 0xf4, 0xbc, 0x60, 0x1f, 0xac, 0x66, 0x88, 0x8b, \
                          0xbf, 0x3b, 0xe9, 0xd1, 0x2e, 0x8c, 0x9d, 0x08, 0xd8, 0x58, 0x7c, 0x65, 0x54, 0xe6, 0x5c, 0xea, \
                          0xd7, 0x61, 0x24, 0x14, 0xd1, 0xb1, 0xe5, 0x7a, 0x88, 0x00, 0x1b, 0xb0, 0x8e, 0x78, 0x7e, 0xa2, \
                          0x34, 0x66, 0xca, 0x71, 0x5d, 0x1a, 0xcb, 0xb2, 0x2b, 0xcf, 0x1d, 0x94, 0x7d, 0x8b, 0x05, 0xf3, \
                          0x6a, 0xa2, 0x47, 0xfe, 0xb4, 0xa5, 0xb0, 0x91, 0x27, 0xf5, 0xa6, 0x1c, 0xae, 0xf1, 0xf4, 0x67, \
                          0x4b, 0x87, 0xe1, 0xa0, 0x68, 0xaf, 0x15, 0xc6, 0x03, 0xae, 0xe1, 0xaf, 0xf8, 0x54, 0xb8, 0xc9, \
                          0x3b, 0xd7, 0xc2, 0x2c, 0x7b, 0x77, 0xea, 0xbb, 0x22, 0x5a, 0xa6, 0x7e, 0xc3, 0x06, 0xfa, 0x80, \
                          0x3d, 0x54, 0x1a, 0xc2, 0x2c, 0xcb, 0x28, 0x3e, 0xa3, 0x15, 0x56, 0x49, 0x43, 0xc8, 0x06, 0x23

#define RSA_TST_KEY_E     0x01, 0x00, 0x01

#define RSA_TST_KEY_D     0x41, 0x09, 0x25, 0xdf, 0x46, 0x57, 0x4d, 0xeb, 0xc7, 0x8e, 0x01, 0x93, 0x1a, 0xc5, 0x66, 0x55, \
                          0xc5, 0x80, 0xbb, 0x2c, 0x10, 0x95, 0xc9, 0xed, 0x1f, 0xab, 0x0b, 0xe1, 0xb9, 0x18, 0x81, 0x02, \
                          0x6e, 0x0c, 0x67, 0x67, 0x0a, 0x6e, 0xd0, 0x7e, 0x08, 0x4c, 0x2e, 0xa8, 0x1a, 0xa6, 0xef, 0x09, \
                          0x14, 0x17, 0x82, 0x50, 0xb9, 0x84, 0x24, 0xa1, 0x53, 0x51, 0x0b, 0x7c, 0x7c, 0x33, 0xdc, 0x31, \
                          0x09, 0x44, 0xf6, 0x36, 0x40, 0x8d, 0xa1, 0xcb, 0x3f, 0x46, 0xb7, 0x88, 0x60, 0x90, 0x46, 0x6f, \
                          0x4f, 0x5d, 0x0a, 0x35, 0x37, 0xc9, 0xa0, 0xa5, 0x09, 0x2e, 0x0f, 0x18, 0xe2, 0x0c, 0x83, 0x98, \
                          0xea, 0x75, 0x6d, 0x7e, 0x6e, 0x42, 0xbb, 0x9c, 0x80, 0x55, 0x28, 0x77, 0x72, 0x34, 0x9a, 0xff, \
                          0x68, 0x4d, 0x29, 0x52, 0x12, 0x77, 0x47, 0xf5, 0x9e, 0xb4, 0x0d, 0xb9, 0xa8, 0x2c, 0x1d, 0x9f, \
                          0xeb, 0x7b, 0x9f, 0xe3, 0x10, 0xae, 0x18, 0x37, 0xc4, 0xda, 0x1e, 0x6c, 0x40, 0xa8, 0xc3, 0x72, \
                          0x32, 0x87, 0x6c, 0x80, 0x8a, 0xea, 0xce, 0x5f, 0xe3, 0x30, 0xdb, 0xb1, 0x98, 0xde, 0x2b, 0x8f, \
                          0xdf, 0xcd, 0xe7, 0x82, 0x9c, 0x9c, 0x24, 0x7a, 0x98, 0xe6, 0x70, 0xca, 0x2e, 0x9d, 0xdc, 0xd2, \
                          0xe9, 0x80, 0x7f, 0x68, 0x2e, 0x5c, 0xc0, 0x8d, 0x4a, 0x4e, 0xbb, 0x0c, 0x34, 0x3a, 0x87, 0x18, \
                          0x3e, 0x6d, 0xdc, 0x71, 0xa8, 0x97, 0xf2, 0xe5, 0x2d, 0x10, 0x45, 0x57, 0x24, 0x6b, 0x4a, 0xa1, \
                          0x96, 0x19, 0x36, 0xc9, 0xae, 0x54, 0x9f, 0xd8, 0xe7, 0xd6, 0xa4, 0xf4, 0xf2, 0x74, 0x46, 0x25, \
                          0xfd, 0x98, 0x5e, 0x37, 0xaa, 0x4d, 0xb5, 0xff, 0x09, 0xbc, 0x01, 0x69, 0x7c, 0x95, 0x75, 0x22, \
                          0x61, 0x30, 0xea, 0x2e, 0x90, 0x42, 0xff, 0xf3, 0xb3, 0x65, 0x3f, 0xb7, 0x44, 0xce, 0xd9, 0x31

#define TST_MSG_SZ  (16)

static void dump_buf(unsigned char *buf, unsigned int buf_sz)
{
	unsigned int idx = 0;

	if (buf_sz < 4)
		return;

	for (idx = 0; idx <= buf_sz - 4; idx += 4) {
		dprintf(CRITICAL, "[0x%x] 0x%x 0x%x 0x%x 0x%x\n", idx,
								*(buf + idx),
								*(buf + idx + 1),
								*(buf + idx + 2),
								*(buf + idx + 3));
	}

	return;
}

void rsa_oaep_sample(void)
{
	unsigned int ret = 0;
	unsigned int idx = 0;

	unsigned char key_n[] = {RSA_TST_KEY_N};
	unsigned char key_e[] = {RSA_TST_KEY_E};
	unsigned char key_d[] = {RSA_TST_KEY_D};

	unsigned int key_n_sz = sizeof(key_n);
	unsigned int key_e_sz = sizeof(key_e);
	unsigned int key_d_sz = sizeof(key_d);

	unsigned char msg[TST_MSG_SZ] = {0};
	unsigned char emsg[RSA_MODULUS_SZ] = {0};
	unsigned int msg_sz = TST_MSG_SZ;
	unsigned int emsg_sz = RSA_MODULUS_SZ;

	dprintf(CRITICAL, "msg = ");
	for (idx = 0; idx < TST_MSG_SZ; idx++) {
		msg[idx] = idx % 16;
		dprintf(CRITICAL, "0x%x ", msg[idx]);
	}
	dprintf(CRITICAL, "\n");

	ret = rsa_oaep_encrypt(msg, msg_sz, key_e, key_e_sz, key_n, key_n_sz, emsg, &emsg_sz);
	if (ret) {
		dprintf(CRITICAL, "rsa_oaep_encrypt test fail(0x%x)\n", ret);
		return;
	}

	dprintf(CRITICAL, "emsg = ");
	for (idx = 0; idx < TST_MSG_SZ; idx++) {
		dprintf(CRITICAL, "0x%x ", emsg[idx]);
	}
	dprintf(CRITICAL, "\n");

    memset(msg, 0x0, TST_MSG_SZ);
	ret = rsa_oaep_decrypt(emsg, emsg_sz, key_d, key_d_sz, key_n, key_n_sz, msg, &msg_sz);
	if (ret) {
		dprintf(CRITICAL, "rsa_oaep_decrypt test fail(0x%x)\n", ret);
		return;
	}

	dprintf(CRITICAL, "dmsg = ");
	for (idx = 0; idx < TST_MSG_SZ; idx++) {
		dprintf(CRITICAL, "0x%x ", msg[idx]);
	}
	dprintf(CRITICAL, "\n");

	for (idx = 0; idx < TST_MSG_SZ; idx++) {
		if (msg[idx] != (idx % 16)) {
			dprintf(CRITICAL, "rsa_oaep test failed\n");
			return;
		}
	}

	dprintf(CRITICAL, "rsa_oaep test pass\n");

	return;
}

